﻿using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace HamcoWcs.Models.ForeEndModels.BI
{
    public static class ChartsHandle
    {
        /// <summary>
        /// 基础类型表格数据转化
        /// </summary>
        /// <param name="t">继承自<see cref="ChartDataBase"/></param>
        /// <param name="title">表的标题</param>
        /// <returns><see cref="ChartModelBase"/>（前端数据）</returns>
        public static ChartModelBase ConvertChartsModel<T>(this T t, string title = "") where T : ChartDataBase
        {
            var chart = new ChartModelBase() { Title = title };
            var type = typeof(T);
            var pis = type.GetProperties();

            foreach (var pi in pis)
            {
                var attr = pi.GetCustomAttributes<DescriptionAttribute>().FirstOrDefault();
                string name = pi.Name;
                if (attr != null) name = attr.Description;
                var value = pi.GetValue(t)?.ToString() ?? "";
                chart.Titles.Add(name);
                chart.Values.Add(value);
            }

            return chart;
        }

        /// <summary>
        /// 饼状表格数据转化
        /// </summary>
        /// <param name="t">继承自<see cref="ChartDataBase"/></param>
        /// <param name="title">表的标题</param>
        /// <returns><see cref="ChartModelBase"/>（前端数据）</returns>
        public static PieChartModel ConvertPieChartsModel<T>(this T t, string title = "") where T : ChartDataBase
        {
            var chart = new PieChartModel() { Title = title };
            var type = typeof(T);
            var pis = type.GetProperties();

            foreach (var pi in pis)
            {
                var attr = pi.GetCustomAttributes<DescriptionAttribute>().FirstOrDefault();
                string name = pi.Name;
                if (attr != null) name = attr.Description;
                var value = pi.GetValue(t)?.ToString() ?? "";
                chart.Series.Add(new PieChartItem()
                {
                    Name = name,
                    Value = value,
                });

            }

            return chart;
        }
    }

    /// <summary>
    /// 报表表格数据基类
    /// </summary>
    public abstract class ChartDataBase
    {

    }
}
